unit uFormFinalizarOS;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  FireDAC.Stan.Async, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, uDM, uFuncoes;

type
  TformFinalizarOS = class(TForm)
    pnlTop: TPanel;
    lb: TLabel;
    lbOS_Id: TLabel;
    pnlBottom: TPanel;
    edtData: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    edtHora: TEdit;
    Label3: TLabel;
    edtImpressor: TEdit;
    Label4: TLabel;
    edtEspalhador: TEdit;
    mmObs: TMemo;
    Label5: TLabel;
    btnConcluir: TButton;
    lbCliente: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btnConcluirClick(Sender: TObject);
  private
    { Private declarations }
    procedure CarregarOS(const pOS_Id: Integer);
    procedure Concluir;
  public
    { Public declarations }
  end;

function FinalizarOS(const pOS_Id: Integer): Boolean;

implementation

{$R *.dfm}

function FinalizarOS(const pOS_Id: Integer): Boolean;
var
  F: TformFinalizarOS;
begin
  F := TformFinalizarOS.Create(nil);
  try
    F.lbOS_Id.Caption := IntToStr(pOS_Id);
    F.CarregarOS(pOS_Id);
    Result := F.ShowModal = mrOk;
  finally
    FreeAndNil(F);
  end;
end;

procedure TformFinalizarOS.btnConcluirClick(Sender: TObject);
begin
  Concluir;
end;

procedure TformFinalizarOS.CarregarOS(const pOS_Id: Integer);
var
  Q: TFDQuery;
begin
  Q := DM.CriarFDQuery;
  try
    Q.Open('SELECT OS.Id, OS.Impressor, OS.Espalhador, OS.Observacao, ' +
     ' Cliente.Nome AS Cliente_Nome FROM OS ' +
     ' JOIN Pessoa AS Cliente ON Cliente.Id = OS.Cliente_Id ' +
     ' WHERE OS.Id = ' + IntToStr(pOS_Id));

    edtImpressor.Text := Q.FieldByName('Impressor').AsString;
    edtEspalhador.Text := Q.FieldByName('Espalhador').AsString;
    lbCliente.Caption := Q.FieldByName('Cliente_Nome').AsString;
    lbCliente.Font.Color := clRed;
    mmObs.Lines.Text := Q.FieldByName('Observacao').AsString;
    Q.Close;
  finally
    FreeAndNil(Q);
  end;
end;

procedure TformFinalizarOS.Concluir;
var
  Q: TFDQuery;
begin
  DM.FDConexao.StartTransaction;
  try
    Q := DM.CriarFDQuery;
    try
      Q.SQL.Text :=
        'UPDATE OS SET Impressor = :Impressor, Espalhador = :Espalhador, ' +
        ' Observacao = :Observacao WHERE Id = :Id';
      Q.ParamByName('Id').AsInteger := StrToInt(lbOS_Id.Caption);
      if Trim(edtImpressor.Text) <> '' then
        Q.ParamByName('Impressor').AsString := edtImpressor.Text
      else
      begin
        Q.ParamByName('Impressor').DataType := ftString;
        Q.ParamByName('Impressor').Value := NULL;
      end;

      if Trim(edtEspalhador.Text) <> '' then
        Q.ParamByName('Espalhador').AsString := edtEspalhador.Text
      else
      begin
        Q.ParamByName('Espalhador').DataType := ftString;
        Q.ParamByName('Espalhador').Value := NULL;
      end;

      if Trim(mmObs.Lines.Text) <> '' then
        Q.ParamByName('Observacao').AsString := mmObs.Lines.Text
      else
      begin
        Q.ParamByName('Observacao').DataType := ftString;
        Q.ParamByName('Observacao').Value := NULL;
      end;

      Q.ExecSQL;

      Q.SQL.Clear;
      Q.SQL.Text :=
        'INSERT INTO OSSituacao(OS_Id, DataRegistro, HoraRegistro, Situacao) VALUES (' +
        ' :OS, :Data, :Hora, :Situacao)';
      Q.ParamByName('OS').AsInteger := StrToInt(lbOS_Id.Caption);
      Q.ParamByName('Data').AsDateTime := StrToDate(edtData.Text);
      Q.ParamByName('Hora').AsDateTime := StrToTime(edtHora.Text);
      Q.ParamByName('Situacao').AsString := 'Finalizado';
      Q.ExecSQL;
    finally
      FreeAndNil(Q);
    end;

    DM.FDConexao.Commit;

    ModalResult := mrOk;
  except
    on E:Exception do
    begin
      DM.FDConexao.Rollback;
      raise Exception.Create('Erro ao finalizar OS ' + E.Message);
    end;
  end;
end;

procedure TformFinalizarOS.FormCreate(Sender: TObject);
begin
  edtData.Text := FormatDateTime('dd/mm/YYYY', now);
  edtHora.Text := FormatDateTime('hh:nn:ss', now);

end;

end.
